#include "PuzzleContent.h"
#include <MultiWidgets\TextWidget.hpp>
#include <MultiWidgets\ImageWidget.hpp>
#include <MultiWidgets/FadeAnimator.hpp>
#include "Puzzle.h"

PuzzleContent::PuzzleContent(){};

void PuzzleContent::init(){
	gameOver = false;
	cleared = false;
	done = false;
	time(&timer);
		
	setBackgroundColor(0,0,0,1);
	setBorderColor(1,1,1,0.7);
	setBorderWidth(1);

	timerWidget = MultiWidgets::create<MultiWidgets::TextWidget>("2:00");
	timerWidget->setInputFlags(INPUT_NONE);
	//b1->eventAddListener("single-tap", "nextContent", this);
	addChild(timerWidget);

	
	//puzzle
	auto puzzle = MultiWidgets::create<Puzzle>();
	puzzle->setSize(height()/2-10,height()/2-10);
	puzzle->setCenterLocation(Nimble::Vector2(width()/3, height()*3/4));
	
	puzzle->init();
	puzzle->eventAddListener("done","puzzleDone",this);
	puzzle->bringToTop();
	addChild(puzzle);

	//logo
	auto logo = MultiWidgets::create<MultiWidgets::ImageWidget>();
	logo->load("data/puzzleLogo.png");
	logo->setSize(height()/2-10,height()/2-10);
	logo->setCenterLocation(Nimble::Vector2(width()/3, height()/4));
	logo->setAutoBringToTop(false);
	addChild(logo);

	//puzzle animation
	Nimble::Vector2f pStartPos(puzzle->location());
	Nimble::Vector2f pStopPos(puzzle->location()-Nimble::Vector2(0,height()/4));
	MultiWidgets::AnimationCurve<Nimble::Vector2f> puzzleCurve;
	puzzleCurve.addKey(0.,pStartPos);
	puzzleCurve.addKey(2,pStopPos);

	MultiWidgets::AnimationClip puzzleClip;
    puzzleClip.setCurve("location", puzzleCurve);
	puzzleClip.setWrapMode(MultiWidgets::AnimationClip::ONCE);
	puzzle->animation().addClip("move", puzzleClip);

	//logo animation
	Nimble::Vector2f lStartPos(logo->location());
	Nimble::Vector2f lStopPos(logo->location()+Nimble::Vector2(0,height()/4));
	MultiWidgets::AnimationCurve<Nimble::Vector2f> logoCurve;
	logoCurve.addKey(0.,lStartPos);
	logoCurve.addKey(2,lStopPos);

	MultiWidgets::AnimationClip logoClip;
    logoClip.setCurve("location", logoCurve);
	logoClip.setWrapMode(MultiWidgets::AnimationClip::ONCE);
	logo->animation().addClip("move", logoClip);

	//puzzleDone();
}

void PuzzleContent::puzzleDone(){
	done = true;
	time(&timer);
	for(auto widget = childBegin(); widget != childEnd(); widget++){
		widget->animation().play("move");
	}
}

void PuzzleContent::eventProcess(const QByteArray & messageId, Radiant::BinaryData & data){

	if(messageId == "nextContent") nextContent();
	else if(messageId == "puzzleDone") puzzleDone();
}

void PuzzleContent::nextContent(){

	Radiant::BinaryData data;
	//data.write(temp);
	eventSend("nextContent",data);

}

void PuzzleContent::update(const MultiWidgets::FrameInfo &frameInfo){
	
	if(gameOver){

		if(cleared){
			time_t currentTime;

			time(&currentTime);
			int diff;
			diff = difftime(currentTime,timer);

			if (diff > 1){

				//TODO: CHANGE THIS FOR A FUNCTION THAT RESETS THE GAME
				nextContent();
			}
		}
		else{
			time_t currentTime;

			time(&currentTime);
			int diff;
			diff = difftime(currentTime,timer);



			if(diff > 5){
				cleared = true;
				time(&timer);
				MultiWidgets::FadeAnimator::fadeOut(*this, 1);

			}
		}

	}
	
	else if(done){

		if(cleared){
			time_t currentTime;

			time(&currentTime);
			int diff;
			diff = difftime(currentTime,timer);

			if (diff > 1){
				nextContent();
			}
		}
		else{
			time_t currentTime;

			time(&currentTime);
			int diff;
			diff = difftime(currentTime,timer);



			if(diff > 5){
				cleared = true;
				time(&timer);
				MultiWidgets::FadeAnimator::fadeOut(*this, 1);

			}
		}

	}
	else{
		time_t currentTime;

		time(&currentTime);
		int diff;
		int seconds;
		int minutes;

		diff = 120 -difftime(currentTime,timer);
		seconds = diff%60;
		minutes = diff/60;

		QString center = ":";
	
		if (seconds <10) center.append("0");
		QString test = QString::number(minutes)+center+QString::number(seconds);
		timerWidget->setText(test);


		if (diff == 0){
			time(&timer);
			gameOver = true;
			setInputFlags(INPUT_NONE);
		}
	}
}